package com.h1909d.weinfo.ui.activity;

import android.media.MediaExtractor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.h1909d.weinfo.R;
import com.h1909d.weinfo.bean.ArticleBean;
import com.h1909d.weinfo.bean.TabBean;
import com.h1909d.weinfo.ui.adapter.RlvJsoupAdapter;
import com.h1909d.weinfo.util.LogUtil;
import com.h1909d.weinfo.util.ThreadManager;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.ArrayList;

public class JsoupActivity extends AppCompatActivity {

    private RecyclerView mRlv;
    public Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            ArrayList<ArticleBean> obj = (ArrayList<ArticleBean>) msg.obj;
            //展示数据
            rlvJsoupAdapter.RlvJsoupAdapter(obj);
        }
    };
    private RlvJsoupAdapter rlvJsoupAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jsoup);
        initView();
        initData();
        initRlv();
    }

    private void initRlv() {
        rlvJsoupAdapter = new RlvJsoupAdapter(this);
        mRlv.setAdapter(rlvJsoupAdapter);
        mRlv.setLayoutManager(new LinearLayoutManager(this));
    }

    private void initData() {
        ThreadManager.getInstance()
                .execute(new Runnable() {
                    @Override
                    public void run() {
                        Document doc = null;
                        try {
                            doc = Jsoup.connect("https://www.wanandroid.com/").get();
                           /* String title = doc.title();
                            LogUtil.print("title:"+title);*/

                            //解析tab数据
                            //查找class是nav的div标签
                            Element tabs = doc.select("div.nav").first();

                            Elements a = tabs.select("a");

                            ArrayList<TabBean> tabList = new ArrayList<>();
                            for (int i = 0; i < a.size(); i++) {
                                Element element = a.get(i);
                                String title = element.text();
                                LogUtil.print("title:"+title);
                                String href = element.attr("href");

                                TabBean tabBean = new TabBean(title, href);
                                tabList.add(tabBean);
                            }

                            //列表数据
                            //查找class是list_article.listArticle的ul标签下的li标签
                            Elements lis = doc.select("ul.list_article.listArticle > li");
                            ArrayList<ArticleBean> articleList = new ArrayList<>();
                            for (int i = 0; i < lis.size(); i++) {
                                //li标签
                                Element element = lis.get(i);
                                //查找class是alink的a标签
                                Element link = element.select("a.alink").first();
                                String href = link.attr("href");
                                String title = link.text();

                                //作者
                                Element authorElement = element.select("span.aauthor").first();
                                String author = authorElement.text();

                                //分类
                                Element chapterElement = element.select("span.achapter").first();
                                String category = chapterElement.text();

                                //时间
                                Element dateElement = element.select("span.aniceDate").first();
                                String date = dateElement.text();
                                ArticleBean articleBean = new ArticleBean(title, href, author, category, date);

                                articleList.add(articleBean);
                            }

                            LogUtil.print(articleList.toString());

                            Message msg = Message.obtain();
                            msg.obj = articleList;
                            mHandler.sendMessage(msg);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }
                });
    }

    private void initView() {
        mRlv = (RecyclerView) findViewById(R.id.rlv);


    }
}
